package com.chunfen.tomcat.designpattern;

public class WinnerState extends State {

    WinnerState(GumballMachine gumballMachine){
        this.gumballMachine = gumballMachine;
    }

    @Override
    protected void insertQuarter() {
        System.out.println("Please wait, we're already giving a gumball");
    }

    @Override
    protected void ejectQuarter() {
        System.out.println("Sorry, you already turned the crank");
    }

    @Override
    protected void turnCrank() {
        System.out.println("Turning twice doesn't get you another gumball!");
    }

    @Override
    protected void dispense() {
        System.out.println("You're a WINNER! You get two gumballs for you quarter!");
        gumballMachine.releaseBall();
        if(gumballMachine.getCount() == 0){
            gumballMachine.setState(gumballMachine.getSoldOutState());
        } else {
            gumballMachine.releaseBall();
            if(gumballMachine.getCount() > 0){
                gumballMachine.setState(gumballMachine.getNoQuarterState());
            } else {
                System.out.println("Oops, out of gumalls!");
                gumballMachine.setState(gumballMachine.getSoldOutState());
            }
        }
    }
}
